package com.miyu.test.ehcache;


import com.alibaba.fastjson.JSONObject;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.expiry.ExpiryPolicy;

import java.time.Duration;


public class Ehcache3 {
    private static CacheManager cacheManager;
    public Ehcache3(String spsName){
        /*初始化内存设置*/
        ResourcePoolsBuilder resourcePoolsBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder()
                // 堆内缓存大小
                .heap(100, MemoryUnit.KB)
                // 堆外缓存大小
                .offheap(100, MemoryUnit.MB)
                // 配置磁盘持久化储存(硬盘存储)用来持久化到磁盘,这里设置为false不启用 true 保存到本地
                .disk(1000, MemoryUnit.MB,true);

        CacheConfiguration<String, String> cacheConfiguration = CacheConfigurationBuilder
                .newCacheConfigurationBuilder(String.class, String.class, resourcePoolsBuilder)
                /*默认过期时间 */
                .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(20)))
                .build();

        // CacheManager管理缓存
        cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                // 硬盘持久化地址
                .with(CacheManagerBuilder.persistence("F:/ehcacheData"))
                // 设置一个默认缓存配置
                .withCache(spsName, cacheConfiguration)
                //创建之后立即初始化
                .build(true);

//        Cache<String, String> mineCache = cacheManager.getCache("spst", String.class, String.class);
//        StringBuilder strTemp = new StringBuilder("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
//        mineCache.put("key", strTemp.toString());
//        String s = mineCache.get("key");
//        System.out.println(">>>>>>"+s);
    }


    /**
     * sps 缓存空间
     * key 缓存key
     * obj 缓存内容
     * @param sps
     * @param key
     * @param obj
     */
    public static void  SetKey(String sps,String key,String obj){
        new Ehcache3(sps);
        System.out.println(">>>>>>>>>>>>>"+cacheManager.getCache(sps, String.class, String.class));
        Cache<String, String> mineCache = cacheManager.getCache(sps, String.class, String.class);
        mineCache.put(key, obj.toString());
        cacheManager.close();
    }

    public static void getKey (String sps,String key){
        Cache<String, Object> mineCache = cacheManager.getCache(sps, String.class, Object.class);
        Object s = mineCache.get(key);
        System.out.println(">>>>>>"+s);
    }
    public static void main(String[] args) {
//        EhcacheUtils.set("test","key","ceshis");
//        System.out.println(EhcacheUtils.get("test","key"));+
        SetKey("testb","key","aaaaaaaaaaaaaaaaaaaaaaaccccccccccccccccc");
        cacheManager.init();
        Cache<String, String> mineCache = cacheManager.getCache("testb", String.class, String.class);
        System.out.println(mineCache.containsKey("key"));
        System.out.println("aaaaaaaaaaaaavvv"+mineCache.get("key"));
        //cacheManager.close();
       // CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true);
       //  SetKey("spst","test","初始化");
//         getKey("spst","test");

    }
}
